﻿using DataAchive.Common;
using DataAchive.Extend;
using DataAchive.Helper;
using DataAchive.Model.Excel.Export;
using NPOI.SS.Formula.Functions;
using SoftCommon;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DataAchive
{
    /// <summary>
    /// ReportShowLocal.xaml 的交互逻辑
    /// </summary>
    public partial class ReportShowLocal : Window
    {
        public ReportShowLocal()
        {
            InitializeComponent();
            ConnectionHelper_Sqlite.GetXmlSetting();

            if (ConnectionHelper_Sqlite.siteSqliteConnections.Count == 0)
            {
                connectionState.Text = "未读取到配置文件有效的sqlite地址设置！！";
                connectionState.Foreground = Brushes.Red;
                return;
            }
            AddressCombo.ItemsSource = ConnectionHelper_Sqlite.siteSqliteConnections;
            AddressCombo.DisplayMemberPath = "Key";
            AddressCombo.SelectedValuePath = "Value";


            #region btn

            reportArea.IsEnabled = false;
            reportArea.IsEnabled = true;
            #endregion
        }


        private void ContainerBtn_Click(object sender, RoutedEventArgs e)
        {
            #region btn
            ContainerBtn.IsEnabled = false;
            #endregion          

            SoftCommon.Log.Write($"containerBtn_Click");          
            
            string path = string.Empty;
            // 创建 FolderBrowserDialog 对话框实例
            using (System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog())
            {
                // 显示对话框，并等待用户选择文件夹
                System.Windows.Forms.DialogResult result = folderDialog.ShowDialog();

                // 如果用户点击了确定按钮，获取选定的文件夹路径并显示在界面上
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    path = folderDialog.SelectedPath;
                }
            }
            SoftCommon.Log.Write($"用户选择文件保存地址：{path}");
            if (string.IsNullOrWhiteSpace(path))
            {
                #region btn
                ContainerBtn.IsEnabled = true;

                #endregion
                return;
            }


            WhereQ whereQ = new WhereQ();
            //whereQ.Date = reportDate.SelectedDate??DateTime.MinValue;
            whereQ.BeginTime = beginDate.TimeValue;
            whereQ.EndTime = endDate.TimeValue;
            //  whereQ.EquitepmentId = EquipmentCombo.SelectedValue?.ToString();
            var selectEquips = EquipmentCombo.SelectedItems.Cast<string>().ToList();
            whereQ.ManyEquitepmentIds = selectEquips;

            string address = AddressCombo.SelectedValue?.ToString();
            string typeExport = exportType.SelectedValue?.ToString();

            // address = "data source=D:\\Database\\tri\\data.db;version=3;";
            SoftCommon.Log.Write($"select address :{address} ");
            address = $"data source={address};version=3;";

            var dstail = NReportHelper.GetAllDataReportInfo(whereQ, address);
            var dtval = NReportHelper.DataReportTrans(dstail.Tables[0]);
            var lsresult = NReportHelper.DicToList(dtval);

            if (typeExport.Equals("CSV", StringComparison.OrdinalIgnoreCase))
            {
                string fileName = $@"{path}\本机信息报表_{DateTime.Now.ToString("yyyyMMddHHmmss")}.csv";
                ReportExport<TwoReport>.ExportCsv(lsresult, fileName);
                MessageBox.Show("导出结束");
            }
            else
            {
                string fileName = $@"{path}\本机信息报表_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
                ReportExport<TwoReport>.ExportXsls(lsresult, fileName);
                MessageBox.Show("导出结束");
            }

            #region btn
            ContainerBtn.IsEnabled = true;

            #endregion
        }

        private void connectBtn_Click(object sender, RoutedEventArgs e)
        {
            var address = AddressCombo.SelectedValue?.ToString();
            var site = AddressCombo.SelectedItem?.ToString();

            if (String.IsNullOrWhiteSpace(address))
            {
                MessageBox.Show("请选择本地机台(请提前配置好机台数据地址信息.)！");
                return;
            }
            if (!File.Exists(address))
            {
                SoftCommon.Log.Write($"机台不存在该{address}数据文件");
                MessageBox.Show($"请确认机台存在数据文件{address}！");
                return;
            }

            #region btn
            reportArea.IsEnabled = true;
            connectBtn.IsEnabled = false;
            #endregion
            connectionState.Text = $"{site}的数据文件读取成功！";
        }

        private void AddressCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var addresscombo = sender as ComboBox;
            if (addresscombo != null)
            {

            }
            #region btn

            connectBtn.IsEnabled = true;
            connectionState.Text = string.Empty;
            reportArea.IsEnabled = false;

            #endregion
        }

        private void TypeCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void AiDefectBtn_Click(object sender, RoutedEventArgs e)
        {
            #region btn
            AiDefectBtn.IsEnabled = false;
            #endregion          

            SoftCommon.Log.Write($"AiDefectBtn_Click");

            string path = string.Empty;
            // 创建 FolderBrowserDialog 对话框实例
            using (System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog())
            {
                // 显示对话框，并等待用户选择文件夹
                System.Windows.Forms.DialogResult result = folderDialog.ShowDialog();

                // 如果用户点击了确定按钮，获取选定的文件夹路径并显示在界面上
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    path = folderDialog.SelectedPath;
                }
            }
            SoftCommon.Log.Write($"用户选择文件保存地址：{path}");
            if (string.IsNullOrWhiteSpace(path))
            {
                #region btn
                AiDefectBtn.IsEnabled = true;

                #endregion
                return;
            }


            WhereQ whereQ = new WhereQ();
            //whereQ.Date = reportDate.SelectedDate??DateTime.MinValue;
            whereQ.BeginTime = beginDate.TimeValue;
            whereQ.EndTime = endDate.TimeValue;
            //  whereQ.EquitepmentId = EquipmentCombo.SelectedValue?.ToString();
            var selectEquips = EquipmentCombo.SelectedItems.Cast<string>().ToList();
            whereQ.ManyEquitepmentIds = selectEquips;

            string address = AddressCombo.SelectedValue?.ToString();
            string typeExport = exportType.SelectedValue?.ToString();

            // address = "data source=D:\\Database\\tri\\data.db;version=3;";
            SoftCommon.Log.Write($"select address :{address} ");
            address = $"data source={address};version=3;";

            var dstail = NReportHelper.GetAllDataReportInfo(whereQ, address);
            var dtval = NReportHelper.DataReportTrans(dstail.Tables[0]);
            var lsresult = NReportHelper.DicToList(dtval);


            // 针对 lsresult处理
            var localDefectData = NAiDefectStaticsReportLocal.GetLocalReportAiDefect(lsresult);

            if (typeExport.Equals("CSV", StringComparison.OrdinalIgnoreCase))
            {
                string fileName = $@"{path}\本机缺陷信息汇总报表_{DateTime.Now.ToString("yyyyMMddHHmmss")}.csv";
                ReportExport<AiDefectStaticViewModel>.ExportCsv(new List<AiDefectStaticViewModel>() { localDefectData }, fileName);
                MessageBox.Show("导出结束");
            }
            else
            {
                string fileName = $@"{path}\本机缺陷信息汇总报表_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
                ReportExport<AiDefectStaticViewModel>.ExportXsls(new List<AiDefectStaticViewModel>() { localDefectData }, fileName);
                MessageBox.Show("导出结束");
            }

            #region btn
            AiDefectBtn.IsEnabled = true;

            #endregion
        }

        private void SmallAllBtn_Click(object sender, RoutedEventArgs e)
        {
            #region btn
            SmallAllBtn.IsEnabled = false;
            #endregion          

            SoftCommon.Log.Write($"SmallAllBtn_Click");

            string path = string.Empty;
            // 创建 FolderBrowserDialog 对话框实例
            using (System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog())
            {
                // 显示对话框，并等待用户选择文件夹
                System.Windows.Forms.DialogResult result = folderDialog.ShowDialog();

                // 如果用户点击了确定按钮，获取选定的文件夹路径并显示在界面上
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    path = folderDialog.SelectedPath;
                }
            }
            SoftCommon.Log.Write($"用户选择文件保存地址：{path}");
            if (string.IsNullOrWhiteSpace(path))
            {
                #region btn
                SmallAllBtn.IsEnabled = true;

                #endregion
                return;
            }


            WhereQ whereQ = new WhereQ();
         
            whereQ.BeginTime = beginDate.TimeValue;
            whereQ.EndTime = endDate.TimeValue;
            
            var selectEquips = EquipmentCombo.SelectedItems.Cast<string>().ToList();
            whereQ.ManyEquitepmentIds = selectEquips;

            string address = AddressCombo.SelectedValue?.ToString();
            string typeExport = exportType.SelectedValue?.ToString();

            // address = "data source=D:\\Database\\tri\\data.db;version=3;";
            SoftCommon.Log.Write($"select address :{address} ");
            address = $"data source={address};version=3;";

            var dstail = NReportHelper.GetAllDataReportInfo(whereQ, address, NReportHelper.SmallReportAll);
            var dtval = NReportHelper.DataReportTrans(dstail.Tables[0]);
            var lsresult = NReportHelper.DicToSmallReportAlls(dtval);

            if (typeExport.Equals("CSV", StringComparison.OrdinalIgnoreCase))
            {
                string fileName = $@"{path}\机台组件信息导出_{DateTime.Now.ToString("yyyyMMddHHmmss")}.csv";
                ReportExport<LocalSmallReportAll>.ExportCsv(lsresult, fileName);
                MessageBox.Show("导出结束");
            }
            else
            {
                string fileName = $@"{path}\机台组件信息导出_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
                ReportExport<LocalSmallReportAll>.ExportXsls(lsresult, fileName);
                MessageBox.Show("导出结束");
            }

            #region btn
            SmallAllBtn.IsEnabled = true;

            #endregion
        }

        private void SmallStaticsBtn_Click(object sender, RoutedEventArgs e)
        {

            #region btn
            SmallStaticsBtn.IsEnabled = false;
            #endregion          

            SoftCommon.Log.Write($"SmallStaticsBtn_Click");

            string path = string.Empty;
            // 创建 FolderBrowserDialog 对话框实例
            using (System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog())
            {
                // 显示对话框，并等待用户选择文件夹
                System.Windows.Forms.DialogResult result = folderDialog.ShowDialog();

                // 如果用户点击了确定按钮，获取选定的文件夹路径并显示在界面上
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    path = folderDialog.SelectedPath;
                }
            }
            SoftCommon.Log.Write($"用户选择文件保存地址：{path}");
            if (string.IsNullOrWhiteSpace(path))
            {
                #region btn
                SmallStaticsBtn.IsEnabled = true;

                #endregion
                return;
            }


            WhereQ whereQ = new WhereQ();
            //whereQ.Date = reportDate.SelectedDate??DateTime.MinValue;
            whereQ.BeginTime = beginDate.TimeValue;
            whereQ.EndTime = endDate.TimeValue;
            //  whereQ.EquitepmentId = EquipmentCombo.SelectedValue?.ToString();
            var selectEquips = EquipmentCombo.SelectedItems.Cast<string>().ToList();
            whereQ.ManyEquitepmentIds = selectEquips;

            string address = AddressCombo.SelectedValue?.ToString();
            string typeExport = exportType.SelectedValue?.ToString();

            // address = "data source=D:\\Database\\tri\\data.db;version=3;";
            SoftCommon.Log.Write($"select address :{address} ");
            address = $"data source={address};version=3;";

            var dstail = NReportHelper.GetLocalRowColReport(whereQ, address);
            var dtval = NReportHelper.DataReportTrans(dstail.Tables[0]);
            var lsresult = NReportHelper.DicToSmallReportColRow(dtval);


            // 针对 lsresult处理
            var localDefectData = NAiDefectStaticsReportAll.GetSmallReportAiDefect(lsresult);

            if (typeExport.Equals("CSV", StringComparison.OrdinalIgnoreCase))
            {
                string fileName = $@"{path}\本机缺陷信息汇总报表_{DateTime.Now.ToString("yyyyMMddHHmmss")}.csv";
                ReportExport<AiDefectStaticViewModel>.ExportCsv(new List<AiDefectStaticViewModel>() { localDefectData }, fileName);
                MessageBox.Show("导出结束");
            }
            else
            {
                string fileName = $@"{path}\本机缺陷信息汇总报表_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
                ReportExport<AiDefectStaticViewModel>.ExportXsls(new List<AiDefectStaticViewModel>() { localDefectData }, fileName);
                MessageBox.Show("导出结束");
            }

            #region btn
            SmallStaticsBtn.IsEnabled = true;

            #endregion
        }

        private void SmallRowColBtn_Click(object sender, RoutedEventArgs e)
        {
            #region btn
            SmallStaticsBtn.IsEnabled = false;
            #endregion          

            SoftCommon.Log.Write($"SmallStaticsBtn_Click");

            string path = string.Empty;
            // 创建 FolderBrowserDialog 对话框实例
            using (System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog())
            {
                // 显示对话框，并等待用户选择文件夹
                System.Windows.Forms.DialogResult result = folderDialog.ShowDialog();

                // 如果用户点击了确定按钮，获取选定的文件夹路径并显示在界面上
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    path = folderDialog.SelectedPath;
                }
            }
            SoftCommon.Log.Write($"用户选择文件保存地址：{path}");
            if (string.IsNullOrWhiteSpace(path))
            {
                #region btn
                SmallStaticsBtn.IsEnabled = true;

                #endregion
                return;
            }


            WhereQ whereQ = new WhereQ();

            whereQ.BeginTime = beginDate.TimeValue;
            whereQ.EndTime = endDate.TimeValue;

            var selectEquips = EquipmentCombo.SelectedItems.Cast<string>().ToList();
            whereQ.ManyEquitepmentIds = selectEquips;

            string address = AddressCombo.SelectedValue?.ToString();
            string typeExport = exportType.SelectedValue?.ToString();

            // address = "data source=D:\\Database\\tri\\data.db;version=3;";
            SoftCommon.Log.Write($"select address :{address} ");
            address = $"data source={address};version=3;";

            var dstail = NReportHelper.GetLocalRowColReport(whereQ, address);
            var dtval = NReportHelper.DataReportTrans(dstail.Tables[0]);
            var lsresult = NReportHelper.DicToSmallReportColRow(dtval);

            if (typeExport.Equals("CSV", StringComparison.OrdinalIgnoreCase))
            {
                string fileName = $@"{path}\本机合并信息导出_{DateTime.Now.ToString("yyyyMMddHHmmss")}.csv";
                ReportExport<SmallReportRowCol>.ExportCsv(lsresult, fileName);
                MessageBox.Show("导出结束");
            }
            else
            {
                string fileName = $@"{path}\本机合并信息导出_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
                ReportExport<SmallReportRowCol>.ExportXsls(lsresult, fileName);
                MessageBox.Show("导出结束");
            }

            #region btn
            SmallStaticsBtn.IsEnabled = true;

            #endregion
        }

        private void aiDefectBaseBtn_Click(object sender, RoutedEventArgs e)
        {
            #region btn
            aiDefectBaseBtn.IsEnabled = false;
            #endregion          

            SoftCommon.Log.Write($"aiDefectBaseBtn_Click");

            string path = string.Empty;
            // 创建 FolderBrowserDialog 对话框实例
            using (System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog())
            {
                // 显示对话框，并等待用户选择文件夹
                System.Windows.Forms.DialogResult result = folderDialog.ShowDialog();

                // 如果用户点击了确定按钮，获取选定的文件夹路径并显示在界面上
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    path = folderDialog.SelectedPath;
                }
            }
            SoftCommon.Log.Write($"用户选择文件保存地址：{path}");
            if (string.IsNullOrWhiteSpace(path))
            {
                #region btn
                aiDefectBaseBtn.IsEnabled = true;

                #endregion
                return;
            }


            WhereQ whereQ = new WhereQ();
            //whereQ.Date = reportDate.SelectedDate??DateTime.MinValue;
            whereQ.BeginTime = beginDate.TimeValue;
            whereQ.EndTime = endDate.TimeValue;
            //  whereQ.EquitepmentId = EquipmentCombo.SelectedValue?.ToString();
            var selectEquips = EquipmentCombo.SelectedItems.Cast<string>().ToList();
            whereQ.ManyEquitepmentIds = selectEquips;

            string address = AddressCombo.SelectedValue?.ToString();
            string typeExport = exportType.SelectedValue?.ToString();

            // address = "data source=D:\\Database\\tri\\data.db;version=3;";
            SoftCommon.Log.Write($"select address :{address} ");
            address = $"data source={address};version=3;";

            var dstail = NReportHelper.GetLocalRowColReport(whereQ, address);
            var dtval = NReportHelper.DataReportTrans(dstail.Tables[0]);
            var lsresult = NReportHelper.DicToSmallReportColRow(dtval);


            // 针对 lsresult处理
            var localDefectData = NAiDefectStaticsReportAllColumn.GetSmallReportAids(lsresult);

            if (typeExport.Equals("CSV", StringComparison.OrdinalIgnoreCase))
            {
                string fileName = $@"{path}\本机缺陷汇总基础信息报表_{DateTime.Now.ToString("yyyyMMddHHmmss")}.csv";
                ReportExport<AiDefectStaticColumnAiC>.ExportCsv( localDefectData , fileName);
                MessageBox.Show("导出结束");
            }
            else
            {
                string fileName = $@"{path}\本机缺陷汇总基础信息报表_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
                ReportExport<AiDefectStaticColumnAiC>.ExportXsls(localDefectData , fileName);
                MessageBox.Show("导出结束");
            }

            #region btn
            aiDefectBaseBtn.IsEnabled = true;

            #endregion
        }

     
    }
}
